//The string "PAYPALISHIRING" is written in a zigzag pattern on a given number o
//f rows like this: (you may want to display this pattern in a fixed font for bett
//er legibility) 
//
// 
//P   A   H   N
//A P L S I I G
//Y   I   R
// 
//
// And then read line by line: "PAHNAPLSIIGYIR" 
//
// Write the code that will take a string and make this conversion given a numbe
//r of rows: 
//
// 
//string convert(string s, int numRows);
// 
//
// 
// Example 1: 
//
// 
//Input: s = "PAYPALISHIRING", numRows = 3
//Output: "PAHNAPLSIIGYIR"
// 
//
// Example 2: 
//
// 
//Input: s = "PAYPALISHIRING", numRows = 4
//Output: "PINALSIGYAHRPI"
//Explanation:
//P     I    N
//A   L S  I G
//Y A   H R
//P     I
// 
//
// Example 3: 
//
// 
//Input: s = "A", numRows = 1
//Output: "A"
// 
//
// 
// Constraints: 
//
// 
// 1 <= s.length <= 1000 
// s consists of English letters (lower-case and upper-case), ',' and '.'. 
// 1 <= numRows <= 1000 
// 
// Related Topics 字符串 
// 👍 1137 👎 0


package leetcode.editor.cn;

import org.junit.Assert;

import java.util.ArrayList;
import java.util.List;

//Java：ZigZag Conversion
class P6ZigzagConversion {
    public static void main(String[] args) {
        Solution solution = new P6ZigzagConversion().new Solution();
        // TO TEST
        Assert.assertEquals(solution.convert("PAHNAPLSIIGYIR", 1),
                "PAHNAPLSIIGYIR");
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String convert(String s, int numRows) {
            List<StringBuilder> list = new ArrayList<>(numRows);
            for (int i = 0; i < numRows; i++) {
                list.add(new StringBuilder());
            }
            int row = 0;
            boolean down = true;
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (row >= numRows - 1) {
                    down = false;
                    row = numRows - 1;
                } else if (row <= 0) {
                    down = true;
                    row = 0;
                }
                list.get(row).append(c);
                row = down ? row + 1 : row - 1;
            }
            StringBuilder sb = new StringBuilder();
            for (StringBuilder characters : list) {
                sb.append(characters);

            }
            return sb.toString();
        }

    }
//leetcode submit region end(Prohibit modification and deletion)

}